function Stock() {
    return require('../models').db.Stock;
}

function Ratings() {
    return require('../models').db.Ratings;
}

function RatingHistory() {
    return require('../models').db.RatingHistory;
}

function ratings() {
    return Stock().findAll().then(sms => {
        let stocks = sms.map(s => s.get({ plain: true }));
        let massPromise = require('../base/MassPromise')();
        for (let stock of stocks) {
            massPromise.addTask(() => {
                return RatingHistory().findAll({
                    where: {
                        StockCode: stock.code
                    },
                    order: [['date', 'DESC']],
                }).then(ratings => {
                    stock.ratingHistory = ratings.map(r => r.get({ plain: true }));
                });
            });
        }
        return massPromise
            .awaitAll()
            .then(() => {
                return stocks;
            });
    });
}

function awaitPromises(promises) {
    promises
}

module.exports = {
    ratings
}